import { BasicForm } from '@/components/ProForm/interface/index';

import { defaultStyle } from '@/basic/style';
import { dayjs } from 'element-plus';
export function useAddForm(refresh: Function) {
  let addForm = reactive<BasicForm>({
    form: {},
    title: '',
    visible: false,
    transitionArr: [
      {
        oldField: 'gender',
        newField: 'gender'
      }
    ],
    options: [
      {
        label: 'id',
        prop: 'id',
        hide: true,
        type: 'input',
        defaultValue: ''
      },
      {
        type: 'input',
        label: 'pid',
        hide: true,
        prop: 'pid',
        defaultValue: ''
      },
      {
        type: 'input',
        label: '所属任务',
        hide: true,
        prop: 'pName',
        defaultValue: '',
        attrs: {}
      },
      {
        type: 'input',
        label: '任务名称',
        hide: false,
        prop: 'taskName',
        defaultValue: '',
        rules: [{ required: true, message: '请输入任务名称', trigger: 'change' }]
      },
      {
        type: 'select',
        label: '负责人',
        hide: false,
        prop: 'principalId',
        defaultValue: '',
        attrs: {
          filterable: true,
          reserveKeyword: true,
          remote: true,
          loading: false,
          filterMethod: key =>
            formRemoteMethod(addForm, 'principalId', getUserList, { ...BasicParams.DEFAULT_PAGEABLE, key }, ['nick', 'id'])
        },
        changeMethod: form => {
          nextTick(() => {
            form.value.executorId = form.value.principalId;
            const children = formGetChildren(addForm, ['principalId']);
            const setChildren = new Map();
            setChildren.set('executorId', children.get('principalId'));
            formSetChildren(addForm, setChildren);
          });
        },
        rules: [{ required: true, message: '请选择负责人', trigger: 'change' }]
      },
      {
        type: 'select',
        label: '执行人',
        hide: false,
        defaultValue: '',
        prop: 'executorId',
        attrs: {
          filterable: true,
          reserveKeyword: true,
          remote: true,
          loading: false,
          filterMethod: key =>
            formRemoteMethod(addForm, 'executorId', getUserList, { ...BasicParams.DEFAULT_PAGEABLE, key }, ['nick', 'id'])
        },
        rules: [{ required: true, message: '请选择执行人', trigger: 'change' }]
      },
      {
        type: 'date-picker',
        label: '计划开始日期',
        hide: false,
        defaultValue: dayjs().format('YYYY-MM-DD'),
        prop: 'planStart',
        attrs: {
          type: 'date',
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD'
        },
        rules: [{ required: true, message: '请选择计划开始时间', trigger: 'change' }]
      },
      {
        type: 'date-picker',
        label: '计划结束时间',
        hide: false,
        defaultValue: '',
        prop: 'planEnd',
        attrs: {
          type: 'date',
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD'
        },
        rules: [{ required: true, message: '请选择计划结束时间', trigger: 'change' }]
      },

      {
        type: 'select',
        label: '汇报周期 ',
        hide: false,
        defaultValue: '1',
        prop: 'reportingCycle',
        children: statusReportingCycle,
        rules: [{ required: true, message: '', trigger: 'change' }]
      },
      {
        type: 'time-select',
        label: '汇报截止时间',
        defaultValue: '23:30',
        hide: false,
        prop: 'reportTime',
        attrs: {
          start: '07:30',
          step: '00:15',
          end: '23:30'
        },
        rules: [{ required: true, message: '', trigger: 'change' }]
      },
      {
        type: 'input',
        label: '超时通知',
        defaultValue: '0',
        hide: false,
        prop: 'delayNotificationMinute',
        attrs: {
          type: 'number',
          placeholder: '没有汇报间隔多久再次通知(分钟)'
        },
        rules: [{ required: true, message: '', trigger: 'change' }]
      },
      {
        type: 'select',
        label: '通知方式 ',
        hide: false,
        defaultValue: 'E_MALI',
        prop: 'notificationWay',
        children: statusNotificationWay,
        rules: [{ required: true, message: '', trigger: 'change' }]
      },
      {
        type: 'input',
        label: 'remark',
        defaultValue: '',
        hide: false,
        prop: 'remark',
        attrs: {
          style: defaultStyle
        }
      }
    ],
    // 提交表单
    commitForm: async form => {
      !form.id ? await taskSave(form) : await taskUpdate(form);
      addForm.visible = false;
      elMessage();
      // proTableBox.value.element.updateKeyChildren(form.targetPid, data);
      refresh();
    }
  });

  return { addForm };
}
